package com.groupbuying.cheetah.order.refundManagement;

import com.groupbuying.cheetah.common.CommonConstants;
import com.groupbuying.cheetah.common.FilterContextHandler;
import com.groupbuying.cheetah.common.PageUtils;
import com.groupbuying.cheetah.common.Query;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.groupbuying.cheetah.order.orderManagement.constans.OrderUshopEnumConstants;
import com.groupbuying.cheetah.order.orderManagement.dao.OrderUshopDao;
import com.groupbuying.cheetah.order.orderManagement.dao.SettleUshopSplitMaininfoDao;
import com.groupbuying.cheetah.order.orderManagement.dto.OrderUshopDTO;
import com.groupbuying.cheetah.order.orderManagement.dto.OrderUshopFirstDTO;
import com.groupbuying.cheetah.order.refundManagement.dao.StoreCheckoutCounterDao;
import com.groupbuying.cheetah.order.refundManagement.dao.StoreMainInfoDao;
import com.groupbuying.cheetah.order.refundManagement.domain.StoreCheckoutCounterDO;
import com.groupbuying.cheetah.order.refundManagement.domain.StoreMainInfoDO;
import com.groupbuying.cheetah.pay.dao.PayInfoDao;
import com.groupbuying.cheetah.pay.dao.PayOrderRelDao;
import com.groupbuying.cheetah.pay.domain.PayInfoDO;
import com.groupbuying.cheetah.pay.domain.PayOrderRelDO;
import com.groupbuying.cheetah.utils.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/refundManagement")
public class RefundManagementController {

    private static final Logger logger = LoggerFactory.getLogger(RefundManagementController.class);

    @Autowired
    private RefundManagementDAO refundManagementDAO;

    @Autowired
    private OrderUshopDao orderUshopDao;

    @Autowired
    private StoreMainInfoDao storeMainInfoDao;

    @Autowired
    private StoreCheckoutCounterDao storeCheckoutCounterDao;

    @Autowired
    private PayOrderRelDao payOrderRelDao;

    @Autowired
    private PayInfoDao payInfoDao;

    @Autowired
    private SettleUshopSplitMaininfoDao settleUshopSplitMaininfoDao;

    /**
     * @api {get} /refundManagement/getList 获取退款订单列表
     * @apiExample
     * author zml
     * tar 用于服务商PC端
     * date 2019-11-2
     * @apiGroup CheetahProducer
     * @apiVersion 1.0.0
     * @apiHeader {String} Authorization 用户授权token
     * @apiParam {String} null 无
     * @apiSuccess {Integer} code 请求结果 0-失败 1-成功
     */
    @GetMapping("/getList")
    public PageUtils refundManagementtest(@RequestParam Map<String, Object> params) {
        String providerId = FilterContextHandler.getName();
        params.put("providerId", providerId);
        Query query = new Query(params);
        List<RefundManagementDO> refundManagementDOList = refundManagementDAO.refundOrderManagement(query);
        //logger.info("测试退款订单查询", refundManagementDOList);
        for (RefundManagementDO refundManagementDO : refundManagementDOList) {
            PayOrderRelDO payOrderRelDO = payOrderRelDao.getByOrderId(refundManagementDO.getOrderId());
            PayInfoDO payInfoDO = payInfoDao.get(payOrderRelDO.getId());
            refundManagementDO.setPayMoney(payInfoDO.getPayMoney().toString());
            refundManagementDO.setPayChannel(payInfoDO.getPayChannel());
            switch (payInfoDO.getPayType()) {
                case "1": {
                    refundManagementDO.setPayType("微信支付");
                }
                break;
                case "2": {
                    refundManagementDO.setPayType("支付宝支付");
                }
                break;
                case "3": {
                    refundManagementDO.setPayType("积分支付");
                    refundManagementDO.setPayMoney(payInfoDO.getPayCur().toString());
                }
                break;
                case "4": {
                    refundManagementDO.setPayType("余额支付");
                    refundManagementDO.setPayMoney(payInfoDO.getPayBalance().toString());
                }
                break;
                default: {
                    refundManagementDO.setPayType("未知");
                }
                break;
            }

            refundManagementDO.setPayCur(payInfoDO.getPayCur().toString());
            refundManagementDO.setPayBalance(payInfoDO.getPayBalance().toString());
            refundManagementDO.setStoreAmount(settleUshopSplitMaininfoDao.getStoreAmountByOrderId(refundManagementDO.getOrderId()));
        }
        int total = refundManagementDAO.count(params);
        PageUtils pageUtils = new PageUtils(refundManagementDOList, total);
        return pageUtils;
    }

    @GetMapping("/list")
    public PageUtils refundManagement(@RequestParam Map<String, Object> params) {

        String providerId = FilterContextHandler.getName();
        params.put("providerId", providerId);
        Query query = new Query(params);
        List<RefundManagementDO> refundManagementDOList = refundManagementDAO.refundManagement(query);
        logger.error("refundManagement refundManagementDOList:" + JsonUtils.objectToJson(refundManagementDOList));
        if (null != refundManagementDOList && refundManagementDOList.size() > 0) {
            for (RefundManagementDO refundDo : refundManagementDOList) {
                if ("余额支付".equals(refundDo.getPayType())) {
                    refundDo.setPayMoney(refundDo.getPayBalance());
                } else if ("积分支付".equals(refundDo.getPayType())) {
                    refundDo.setPayMoney(refundDo.getPayCur());
                }
            }
        }
        logger.error("refundManagement 转换后的refundManagementDOList:" + JsonUtils.objectToJson(refundManagementDOList));
        int total = refundManagementDAO.count(params);
        PageUtils pageUtils = new PageUtils(refundManagementDOList, total);
        return pageUtils;

    }

    @GetMapping("/queryStore")
    public List<Map<String, String>> queryStore(@RequestParam Map<String, Object> params) {

        String providerId = FilterContextHandler.getName();
        params.put("providerId", providerId);
        List<Map<String, String>> mapList = refundManagementDAO.queryStore(params);
        return mapList;

    }

}
